package com.ruoyi.common.utils.appsdks;

import cn.hutool.core.map.MapBuilder;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.aliyun.dysmsapi20170525.models.*;
import com.aliyun.teaopenapi.models.*;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author ab785
 * createTime: 2021-4-12.
 * description: 阿里云发送验证码服务
 */
@Slf4j
@Configuration
@ConfigurationProperties(prefix = "aliyun")
public class AliyunUtils {

    /**
     * accessKeyId
     */
    private static String accessKeyId;

    /**
     * accessKeySecret
     */
    private static String accessKeySecret;

    /**
     * 短信模板签名
     */
    private static String signName;

    /**
     * 短信模板编码
     */
    private static String templateCode;

    /**
     * 发送验证码
     *
     * @param code 验证码值
     * @return 发送结果
     */
    public static AjaxResult sendCode(String phone, String code) {
        try {
            Config config = new Config()
                    // 您的AccessKey ID
                    .setAccessKeyId(accessKeyId)
                    // 您的AccessKey Secret
                    .setAccessKeySecret(accessKeySecret);
            // 访问的域名
            config.endpoint = "dysmsapi.aliyuncs.com";
            com.aliyun.dysmsapi20170525.Client client = new com.aliyun.dysmsapi20170525.Client(config);
            SendSmsRequest sendSmsRequest = new SendSmsRequest();

            //设置手机号
            sendSmsRequest.setPhoneNumbers(phone);
            //签名名称
            sendSmsRequest.setSignName(signName);
            //模板编码
            sendSmsRequest.setTemplateCode(templateCode);
            //模板参数
            Map<Object, Object> params = MapBuilder.create().put("code", code).build();
            sendSmsRequest.setTemplateParam(JSON.toJSONString(params));
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponseBody sendResult = client.sendSms(sendSmsRequest).getBody();

            AjaxResult ajax = AjaxResult.success();
            if (sendResult.getCode().equals("OK")) {
                ajax.setSuccessMsg(sendResult.getMessage());
            } else {
                ajax.setErrorMsg(sendResult.getMessage());
                ajax.put("errorCode", sendResult.getCode());
            }
            System.out.println(JSON.toJSONString(sendResult));
            return ajax;
        } catch (Exception e) {
            log.error("验证码发送失败", e);
            throw new ServiceException(StrUtil.isEmpty(e.getMessage()) ? "请检查服务器网络" : e.getMessage());
        }
    }

    public void setAccessKeyId(String accessKeyId) {
        AliyunUtils.accessKeyId = accessKeyId;
    }

    public void setAccessKeySecret(String accessKeySecret) {
        AliyunUtils.accessKeySecret = accessKeySecret;
    }

    public void setSignName(String signName) {
        AliyunUtils.signName = signName;
    }

    public void setTemplateCode(String templateCode) {
        AliyunUtils.templateCode = templateCode;
    }
}
